package org.git.movie.config;

import com.alibaba.csp.sentinel.adapter.servlet.CommonFilter;
import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
import com.alibaba.csp.sentinel.adapter.servlet.config.WebServletConfig;
import org.git.movie.filters.TraceIdFilter;
import org.git.movie.helper.UrlBlockHandlerHelper;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;

/**
 * @author sqyc
 */
@Configuration
public class FilterConfig {

    @Bean
    public FilterRegistrationBean rraceIdFilter() {
        FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();

        TraceIdFilter traceIdFilter = new TraceIdFilter();

        registration.setFilter(traceIdFilter);
        registration.addUrlPatterns("/*");
        registration.setName("traceIdFilter");
        registration.setOrder(1);

        return registration;
    }

    /**
     * 参考文档：https://github.com/alibaba/Sentinel/wiki/%E4%B8%BB%E6%B5%81%E6%A1%86%E6%9E%B6%E7%9A%84%E9%80%82%E9%85%8D#spring-cloud
     * @return
     */
    @Bean
    public FilterRegistrationBean sentinelFilterRegistration() {
        FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();

        CommonFilter commonFilter = new CommonFilter();

        registration.setFilter(commonFilter);
        registration.addUrlPatterns("/*");
        registration.setName("sentinelFilter");
        registration.setOrder(2);

        WebServletConfig.setBlockPage("/sentinel/blockPage");



        return registration;
    }


}
